package com.library.statistic.dao;

import com.framework.BaseDao;
import com.library.entity.Statistic;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Repository
public class StatisticDaoImpl extends BaseDao<Statistic,String> {

	public Statistic getStatisticById(Integer statisticId){
		try{
			return super.get(statisticId);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*根据参数查询list*/
	public List<Statistic> findStatisticsByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Statistic s");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and s."+ key + "=?");
					else{
						hql.append(" and s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" s."+ key + "=?");
					else{
						hql.append(" s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				}
				++i;
				++j;
			}
		}
		try{
			return super.findByProperty(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*根据参数查询one*/
	public Statistic findStatisticByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Statistic s");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and s."+ key + "=?");
					else{
						hql.append(" and s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" s."+ key + "=?");
					else{
						hql.append(" s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				}
				++i;
				++j;
			}
		}
		try{
			return super.findOne(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查找条件统计数量*/
	public Long findCountByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Statistic s");
//		Object[] params = new Object[paramsMap.size()];
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and s."+ key + "=?");
					else{
						hql.append(" and s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" s."+ key + "=?");
					else{
						hql.append(" s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				}
				++i;
				++j;
			}
		}
		try{
			return this.getStatisticCount(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*按条件查询数量*/
	public Long getStatisticCount(String hql,Object[] params){
		try{
			return super.findCountByPage("select count(*) " + hql,params);
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}


	public void saveStatistic(Statistic statistic){
		try {
			this.save(statistic);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateStatistic(Statistic statistic){
		try {
			this.update(statistic);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteStatistic(Statistic statistic){
		try {
			this.delete(statistic);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
